{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd;import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [12]: data = pd.DataFrame({\n",
    "....: 'x0': [1, 2, 3, 4, 5],\n",
    "....: 'x1': [0.01, -0.01, 0.25, -4.1, 0.],\n",
    "....: 'y': [-1.5, 0., 3.6, 1.3, -2.]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x0</th>\n",
       "      <th>x1</th>\n",
       "      <th>y</th>\n",
       "      <th>category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.01</td>\n",
       "      <td>-1.5</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>0.0</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.25</td>\n",
       "      <td>3.6</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>-4.10</td>\n",
       "      <td>1.3</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   x0    x1    y category\n",
       "0   1  0.01 -1.5        a\n",
       "1   2 -0.01  0.0        b\n",
       "2   3  0.25  3.6        a\n",
       "3   4 -4.10  1.3        a\n",
       "4   5  0.00 -2.0        b"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "In [24]: data['category'] = pd.Categorical(['a', 'b', 'a', 'a', 'b'],\n",
    "....: categories=['a', 'b'])\n",
    "In [25]: data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "dummies = pd.get_dummies(data.category)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b\n",
       "0  1  0\n",
       "1  0  1\n",
       "2  1  0\n",
       "3  1  0\n",
       "4  0  1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dummies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dummies = data.drop(columns='category').join(dummies)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x0</th>\n",
       "      <th>x1</th>\n",
       "      <th>y</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.01</td>\n",
       "      <td>-1.5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.25</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>-4.10</td>\n",
       "      <td>1.3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   x0    x1    y  a  b\n",
       "0   1  0.01 -1.5  1  0\n",
       "1   2 -0.01  0.0  0  1\n",
       "2   3  0.25  3.6  1  0\n",
       "3   4 -4.10  1.3  1  0\n",
       "4   5  0.00 -2.0  0  1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_dummies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import patsy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [29]: data = pd.DataFrame({\n",
    "....: 'x0': [1, 2, 3, 4, 5],\n",
    "....: 'x1': [0.01, -0.01, 0.25, -4.1, 0.],\n",
    "....: 'y': [-1.5, 0., 3.6, 1.3, -2.]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('y~x0+x1',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (5, 1)\n",
       "     y\n",
       "  -1.5\n",
       "   0.0\n",
       "   3.6\n",
       "   1.3\n",
       "  -2.0\n",
       "  Terms:\n",
       "    'y' (column 0)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (5, 3)\n",
       "  Intercept  x0     x1\n",
       "          1   1   0.01\n",
       "          1   2  -0.01\n",
       "          1   3   0.25\n",
       "          1   4  -4.10\n",
       "          1   5   0.00\n",
       "  Terms:\n",
       "    'Intercept' (column 0)\n",
       "    'x0' (column 1)\n",
       "    'x1' (column 2)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.5],\n",
       "       [ 0. ],\n",
       "       [ 3.6],\n",
       "       [ 1.3],\n",
       "       [-2. ]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.  ,  1.  ,  0.01],\n",
       "       [ 1.  ,  2.  , -0.01],\n",
       "       [ 1.  ,  3.  ,  0.25],\n",
       "       [ 1.  ,  4.  , -4.1 ],\n",
       "       [ 1.  ,  5.  ,  0.  ]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.\n",
      "To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "coef,resid,_,_ = np.linalg.lstsq(X,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.31290976],\n",
       "       [-0.07910564],\n",
       "       [-0.26546384]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd;import numpy as np;import patsy;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [29]: data = pd.DataFrame({\n",
    "....: 'x0': [1, 2, 3, 4, 5],\n",
    "....: 'x1': [0.01, -0.01, 0.25, -4.1, 0.],\n",
    "....: 'y': [-1.5, 0., 3.6, 1.3, -2.]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('y~x0+np.log(np.abs(x1)+1)',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(DesignMatrix with shape (5, 1)\n",
       "      y\n",
       "   -1.5\n",
       "    0.0\n",
       "    3.6\n",
       "    1.3\n",
       "   -2.0\n",
       "   Terms:\n",
       "     'y' (column 0),\n",
       " DesignMatrix with shape (5, 3)\n",
       "   Intercept  x0  np.log(np.abs(x1) + 1)\n",
       "           1   1                 0.00995\n",
       "           1   2                 0.00995\n",
       "           1   3                 0.22314\n",
       "           1   4                 1.62924\n",
       "           1   5                 0.00000\n",
       "   Terms:\n",
       "     'Intercept' (column 0)\n",
       "     'x0' (column 1)\n",
       "     'np.log(np.abs(x1) + 1)' (column 2))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y,X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('y~standardize(x0) + center(x1)',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (5, 1)\n",
       "     y\n",
       "  -1.5\n",
       "   0.0\n",
       "   3.6\n",
       "   1.3\n",
       "  -2.0\n",
       "  Terms:\n",
       "    'y' (column 0)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (5, 3)\n",
       "  Intercept  standardize(x0)  center(x1)\n",
       "          1         -1.41421        0.78\n",
       "          1         -0.70711        0.76\n",
       "          1          0.00000        1.02\n",
       "          1          0.70711       -3.33\n",
       "          1          1.41421        0.77\n",
       "  Terms:\n",
       "    'Intercept' (column 0)\n",
       "    'standardize(x0)' (column 1)\n",
       "    'center(x1)' (column 2)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [46]: new_data = pd.DataFrame({\n",
    "....: 'x0': [6, 7, 8, 9],\n",
    "....: 'x1': [3.1, -0.5, 0, 2.3],\n",
    "....: 'y': [1, 2, 3, 4]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_X = patsy.build_design_matrices([X.design_info],new_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[DesignMatrix with shape (4, 3)\n",
       "   Intercept  standardize(x0)  center(x1)\n",
       "           1          2.12132        3.87\n",
       "           1          2.82843        0.27\n",
       "           1          3.53553        0.77\n",
       "           1          4.24264        3.07\n",
       "   Terms:\n",
       "     'Intercept' (column 0)\n",
       "     'standardize(x0)' (column 1)\n",
       "     'center(x1)' (column 2)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x0</th>\n",
       "      <th>x1</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.01</td>\n",
       "      <td>-1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.25</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>-4.10</td>\n",
       "      <td>1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   x0    x1    y\n",
       "0   1  0.01 -1.5\n",
       "1   2 -0.01  0.0\n",
       "2   3  0.25  3.6\n",
       "3   4 -4.10  1.3\n",
       "4   5  0.00 -2.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('y~I(x0+x1)',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (5, 2)\n",
       "  Intercept  I(x0 + x1)\n",
       "          1        1.01\n",
       "          1        1.99\n",
       "          1        3.25\n",
       "          1       -0.10\n",
       "          1        5.00\n",
       "  Terms:\n",
       "    'Intercept' (column 0)\n",
       "    'I(x0 + x1)' (column 1)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [51]: data = pd.DataFrame({\n",
    "....: 'key1': ['a', 'a', 'b', 'b', 'a', 'b', 'a', 'b'],\n",
    "....: 'key2': [0, 1, 0, 1, 0, 1, 0, 0],\n",
    "....: 'v1': [1, 2, 3, 4, 5, 6, 7, 8],\n",
    "....: 'v2': [-1, 0, 2.5, -0.5, 4.0, -1.2, 0.2, -1.7]\n",
    "....: })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('v2~key1',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (8, 2)\n",
       "  Intercept  key1[T.b]\n",
       "          1          0\n",
       "          1          0\n",
       "          1          1\n",
       "          1          1\n",
       "          1          0\n",
       "          1          1\n",
       "          1          0\n",
       "          1          1\n",
       "  Terms:\n",
       "    'Intercept' (column 0)\n",
       "    'key1' (column 1)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('v2~C(key2)',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (8, 2)\n",
       "  Intercept  C(key2)[T.1]\n",
       "          1             0\n",
       "          1             1\n",
       "          1             0\n",
       "          1             1\n",
       "          1             0\n",
       "          1             1\n",
       "          1             0\n",
       "          1             0\n",
       "  Terms:\n",
       "    'Intercept' (column 0)\n",
       "    'C(key2)' (column 1)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>v1</th>\n",
       "      <th>v2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>-0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>-1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>b</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>-1.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1  key2  v1   v2\n",
       "0    a     0   1 -1.0\n",
       "1    a     1   2  0.0\n",
       "2    b     0   3  2.5\n",
       "3    b     1   4 -0.5\n",
       "4    a     0   5  4.0\n",
       "5    b     1   6 -1.2\n",
       "6    a     0   7  0.2\n",
       "7    b     0   8 -1.7"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    " data['key2'] = data['key2'].map({0: 'zero', 1: 'one'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>v1</th>\n",
       "      <th>v2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>zero</td>\n",
       "      <td>1</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>one</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b</td>\n",
       "      <td>zero</td>\n",
       "      <td>3</td>\n",
       "      <td>2.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>one</td>\n",
       "      <td>4</td>\n",
       "      <td>-0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>zero</td>\n",
       "      <td>5</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>one</td>\n",
       "      <td>6</td>\n",
       "      <td>-1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>a</td>\n",
       "      <td>zero</td>\n",
       "      <td>7</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>b</td>\n",
       "      <td>zero</td>\n",
       "      <td>8</td>\n",
       "      <td>-1.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1  key2  v1   v2\n",
       "0    a  zero   1 -1.0\n",
       "1    a   one   2  0.0\n",
       "2    b  zero   3  2.5\n",
       "3    b   one   4 -0.5\n",
       "4    a  zero   5  4.0\n",
       "5    b   one   6 -1.2\n",
       "6    a  zero   7  0.2\n",
       "7    b  zero   8 -1.7"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('v2~key1+key2',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (8, 3)\n",
       "  Intercept  key1[T.b]  key2[T.zero]\n",
       "          1          0             1\n",
       "          1          0             0\n",
       "          1          1             1\n",
       "          1          1             0\n",
       "          1          0             1\n",
       "          1          1             0\n",
       "          1          0             1\n",
       "          1          1             1\n",
       "  Terms:\n",
       "    'Intercept' (column 0)\n",
       "    'key1' (column 1)\n",
       "    'key2' (column 2)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "y,X = patsy.dmatrices('v2~key1+key2+key1:key2',data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DesignMatrix with shape (8, 4)\n",
       "  Intercept  key1[T.b]  key2[T.zero]  key1[T.b]:key2[T.zero]\n",
       "          1          0             1                       0\n",
       "          1          0             0                       0\n",
       "          1          1             1                       1\n",
       "          1          1             0                       0\n",
       "          1          0             1                       0\n",
       "          1          1             0                       0\n",
       "          1          0             1                       0\n",
       "          1          1             1                       1\n",
       "  Terms:\n",
       "    'Intercept' (column 0)\n",
       "    'key1' (column 1)\n",
       "    'key2' (column 2)\n",
       "    'key1:key2' (column 3)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>v1</th>\n",
       "      <th>v2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>zero</td>\n",
       "      <td>1</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>one</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b</td>\n",
       "      <td>zero</td>\n",
       "      <td>3</td>\n",
       "      <td>2.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>one</td>\n",
       "      <td>4</td>\n",
       "      <td>-0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>zero</td>\n",
       "      <td>5</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>one</td>\n",
       "      <td>6</td>\n",
       "      <td>-1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>a</td>\n",
       "      <td>zero</td>\n",
       "      <td>7</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>b</td>\n",
       "      <td>zero</td>\n",
       "      <td>8</td>\n",
       "      <td>-1.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1  key2  v1   v2\n",
       "0    a  zero   1 -1.0\n",
       "1    a   one   2  0.0\n",
       "2    b  zero   3  2.5\n",
       "3    b   one   4 -0.5\n",
       "4    a  zero   5  4.0\n",
       "5    b   one   6 -1.2\n",
       "6    a  zero   7  0.2\n",
       "7    b  zero   8 -1.7"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\statsmodels\\compat\\pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n",
      "  from pandas.core import datetools\n"
     ]
    }
   ],
   "source": [
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.formula.api as smf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dnorm(mean, variance, size=1):\n",
    "    if isinstance(size, int):\n",
    "        size = size,\n",
    "    return mean + np.sqrt(variance) * np.random.randn(*size)\n",
    "# For reproducibility\n",
    "np.random.seed(12345)\n",
    "N = 100\n",
    "X = np.c_[dnorm(0, 0.4, size=N),\n",
    "dnorm(0, 0.6, size=N),\n",
    "dnorm(0, 0.2, size=N)]\n",
    "eps = dnorm(0, 0.1, size=N)\n",
    "beta = [0.1, 0.3, 0.5]\n",
    "y = np.dot(X, beta) + eps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100,)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 3)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\statsmodels\\compat\\pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n",
      "  from pandas.core import datetools\n"
     ]
    }
   ],
   "source": [
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.formula.api as smf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "def dnorm(mean, variance, size=1):\n",
    "    if isinstance(size, int):\n",
    "        size = size,\n",
    "    return mean + np.sqrt(variance) * np.random.randn(*size)\n",
    "# For reproducibility\n",
    "np.random.seed(12345)\n",
    "N = 100\n",
    "X = np.c_[dnorm(0, 0.4, size=N),\n",
    "dnorm(0, 0.6, size=N),\n",
    "dnorm(0, 0.2, size=N)]\n",
    "eps = dnorm(0, 0.1, size=N)\n",
    "beta = [0.1, 0.3, 0.5]\n",
    "y = np.dot(X, beta) + eps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.12946849, -1.21275292,  0.50422488],\n",
       "       [ 0.30291036, -0.43574176, -0.25417986],\n",
       "       [-0.32852189, -0.02530153,  0.13835097],\n",
       "       [-0.35147471, -0.71960511, -0.25821463],\n",
       "       [ 1.2432688 , -0.37379916, -0.52262905]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.42786349, -0.67348041, -0.09087764, -0.48949442, -0.12894109])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_model = sm.add_constant(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -0.12946849, -1.21275292,  0.50422488],\n",
       "       [ 1.        ,  0.30291036, -0.43574176, -0.25417986],\n",
       "       [ 1.        , -0.32852189, -0.02530153,  0.13835097],\n",
       "       [ 1.        , -0.35147471, -0.71960511, -0.25821463],\n",
       "       [ 1.        ,  1.2432688 , -0.37379916, -0.52262905]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_model[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = sm.OLS(y,X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = model.fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.17826108, 0.22303962, 0.50095093])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      y   R-squared:                       0.430\n",
      "Model:                            OLS   Adj. R-squared:                  0.413\n",
      "Method:                 Least Squares   F-statistic:                     24.42\n",
      "Date:                Mon, 18 Mar 2019   Prob (F-statistic):           7.44e-12\n",
      "Time:                        08:52:41   Log-Likelihood:                -34.305\n",
      "No. Observations:                 100   AIC:                             74.61\n",
      "Df Residuals:                      97   BIC:                             82.42\n",
      "Df Model:                           3                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "x1             0.1783      0.053      3.364      0.001       0.073       0.283\n",
      "x2             0.2230      0.046      4.818      0.000       0.131       0.315\n",
      "x3             0.5010      0.080      6.237      0.000       0.342       0.660\n",
      "==============================================================================\n",
      "Omnibus:                        4.662   Durbin-Watson:                   2.201\n",
      "Prob(Omnibus):                  0.097   Jarque-Bera (JB):                4.098\n",
      "Skew:                           0.481   Prob(JB):                        0.129\n",
      "Kurtosis:                       3.243   Cond. No.                         1.74\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
     ]
    }
   ],
   "source": [
    "print(results.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame(X,columns=['col0','col1','col2'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "data['y'] = y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col0</th>\n",
       "      <th>col1</th>\n",
       "      <th>col2</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.129468</td>\n",
       "      <td>-1.212753</td>\n",
       "      <td>0.504225</td>\n",
       "      <td>0.427863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.302910</td>\n",
       "      <td>-0.435742</td>\n",
       "      <td>-0.254180</td>\n",
       "      <td>-0.673480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.328522</td>\n",
       "      <td>-0.025302</td>\n",
       "      <td>0.138351</td>\n",
       "      <td>-0.090878</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       col0      col1      col2         y\n",
       "0 -0.129468 -1.212753  0.504225  0.427863\n",
       "1  0.302910 -0.435742 -0.254180 -0.673480\n",
       "2 -0.328522 -0.025302  0.138351 -0.090878"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = smf.ols('y~col1 + col2 + col0',data=data).fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Intercept    0.033559\n",
       "col1         0.224826\n",
       "col2         0.514808\n",
       "col0         0.176149\n",
       "dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Intercept    0.952188\n",
       "col1         4.850730\n",
       "col2         6.303971\n",
       "col0         3.319754\n",
       "dtype: float64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.tvalues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   -0.002327\n",
       "1   -0.141904\n",
       "2    0.041226\n",
       "3   -0.323070\n",
       "4   -0.100535\n",
       "dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.predict(data[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.42786349, -0.67348041, -0.09087764, -0.48949442, -0.12894109])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_x = 4\n",
    "import random\n",
    "values = [init_x, init_x]\n",
    "N = 1000\n",
    "b0 = 0.8\n",
    "b1 = -0.4\n",
    "noise = dnorm(0, 0.1, N)\n",
    "for i in range(N):\n",
    "    new_x = values[-1] * b0 + values[-2] * b1 + noise[i]\n",
    "    values.append(new_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "MAXLAGS = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = sm.tsa.AR(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = model.fit(MAXLAGS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4,\n",
       " 4,\n",
       " 1.8977509636904242,\n",
       " 0.08686526220610424,\n",
       " -0.5769469132535335,\n",
       " -0.4995023802308947,\n",
       " 0.27887732380859565,\n",
       " 0.601921099044195,\n",
       " 0.5143399251963061,\n",
       " 0.23230090795613595,\n",
       " -0.2270414299565975,\n",
       " -0.4532949571877241,\n",
       " 0.7327960955212159,\n",
       " 1.070985691452315,\n",
       " 0.2552405778514706,\n",
       " -0.5773466745933833,\n",
       " -0.9132080351513938,\n",
       " -0.7838390995099828,\n",
       " -0.6939551640304977,\n",
       " -0.09888273000972089,\n",
       " -0.3034477253363709,\n",
       " -0.43493326308961244,\n",
       " -0.3892865103111405,\n",
       " -0.3309481004900743,\n",
       " -0.11903625213901042,\n",
       " 0.11118797731397469,\n",
       " 0.2225586853898911,\n",
       " 0.5227752002595569,\n",
       " 0.736594865340029,\n",
       " 0.6495328948232221,\n",
       " -0.2585809973956484,\n",
       " -0.9573733212451723,\n",
       " -0.5684539829832487,\n",
       " 0.03721869341955729,\n",
       " 0.1312652116183619,\n",
       " 0.2259141777403416,\n",
       " 0.03780162008649546,\n",
       " -0.7648180294491388,\n",
       " -0.6059273357093189,\n",
       " -0.02387419489558526,\n",
       " 0.8139615956084674,\n",
       " 1.315448874642104,\n",
       " 0.6491774717435169,\n",
       " 0.23422352019432813,\n",
       " -0.0314938971513281,\n",
       " 0.07941428955016891,\n",
       " -0.2597791282232123,\n",
       " -0.7137736446174251,\n",
       " -0.2947143719037799,\n",
       " 0.1764894065051516,\n",
       " -0.35637481305796315,\n",
       " -0.19599054026722212,\n",
       " -0.3021644530970059,\n",
       " -0.23280941192000892,\n",
       " 0.0636566938501768,\n",
       " -0.046682206107812985,\n",
       " -0.1770085960130648,\n",
       " 0.05542622070224297,\n",
       " -0.2189870807065061,\n",
       " 0.24281310323048977,\n",
       " -0.27511642672471803,\n",
       " -0.6055020901644366,\n",
       " -0.16616994685366757,\n",
       " -0.39020607405105945,\n",
       " -0.10095754210704855,\n",
       " 0.01736193669994897,\n",
       " -0.16249647493299813,\n",
       " -0.10328541773320485,\n",
       " -0.11826483005741993,\n",
       " 0.08385438749805127,\n",
       " 0.29521667798743134,\n",
       " 0.38196626012764767,\n",
       " 0.21328323844427224,\n",
       " -0.0777665942578419,\n",
       " -0.37739771633872804,\n",
       " -0.21378456554588451,\n",
       " -0.18457301186525552,\n",
       " 0.06374318627767812,\n",
       " -0.16513657639825605,\n",
       " -0.18374280302572332,\n",
       " -0.6943237022484156,\n",
       " -0.036714928498514265,\n",
       " 0.726622464825147,\n",
       " 0.7624756865660068,\n",
       " 0.23492062235229597,\n",
       " 0.15562438130533787,\n",
       " 0.057032112464277473,\n",
       " -0.6087096090766337,\n",
       " -0.8142417845451893,\n",
       " -0.3825902294899999,\n",
       " 0.06033140545812305,\n",
       " 0.04979006129163199,\n",
       " -0.18677498654230093,\n",
       " 0.06656161472878158,\n",
       " -0.0689073805493888,\n",
       " 0.21433012820168407,\n",
       " 0.43620369053101243,\n",
       " 0.24529603448362142,\n",
       " 0.7492925128606548,\n",
       " 0.6814065082389407,\n",
       " 0.7164008976111947,\n",
       " 0.7003491574772843,\n",
       " 0.03280488689023897,\n",
       " -0.358762224368176,\n",
       " -0.8539463002456682,\n",
       " -0.3407783707383257,\n",
       " 0.15829075689675892,\n",
       " -0.16021288875445977,\n",
       " -0.3497923994734585,\n",
       " -0.17728122802719706,\n",
       " 0.5383112931232198,\n",
       " 0.19450453936328949,\n",
       " -0.2555709691479625,\n",
       " -0.5120638801263175,\n",
       " 0.07889864683088288,\n",
       " 0.42722817021152854,\n",
       " -0.1286612415733599,\n",
       " -0.209040554575728,\n",
       " 0.07514040473961708,\n",
       " 0.3162878105992152,\n",
       " 0.2974384583896937,\n",
       " 0.14680097273083864,\n",
       " -0.3972948403220842,\n",
       " -1.3092205669839097,\n",
       " -0.6877695746186626,\n",
       " 0.012734462938684542,\n",
       " 0.6956396612131259,\n",
       " 0.01796275976396189,\n",
       " 0.08065684200490508,\n",
       " 0.7090483982611805,\n",
       " 0.4586906734966019,\n",
       " -0.21641032255914616,\n",
       " -0.1427415775090531,\n",
       " -0.2342384925359708,\n",
       " -0.33656804787711964,\n",
       " -0.008329069401536077,\n",
       " 0.24110812356286995,\n",
       " 0.25587429219948243,\n",
       " 0.38318994172914,\n",
       " 0.18817978256723203,\n",
       " -0.22926651972041387,\n",
       " -0.2913384034157926,\n",
       " -0.5021915632901712,\n",
       " -0.4435774499015228,\n",
       " -0.2770932372683138,\n",
       " -0.19408830451057849,\n",
       " -0.003746123345894803,\n",
       " 0.12324082993615221,\n",
       " 0.24074607970067985,\n",
       " 0.1839458135949507,\n",
       " 0.13071517438363917,\n",
       " -0.2664622488641798,\n",
       " 0.05145034745003485,\n",
       " -0.018405629214412134,\n",
       " -0.31592545778484227,\n",
       " 0.33373409509525465,\n",
       " 0.10146995553271182,\n",
       " 0.1692117940443575,\n",
       " -0.18734152716418684,\n",
       " 0.509999931955207,\n",
       " 0.2243364626837795,\n",
       " 0.13793425791598712,\n",
       " 0.21780832731412716,\n",
       " 0.5900012221151391,\n",
       " 0.9985108817492514,\n",
       " 0.7345948803653328,\n",
       " 0.3483431729795732,\n",
       " 0.5104117156724031,\n",
       " -0.10706674661942411,\n",
       " -0.2602199713519806,\n",
       " -0.3358478756703378,\n",
       " -0.6191347128016624,\n",
       " -0.30153760023945253,\n",
       " -0.13602240179359884,\n",
       " -0.15165412982534565,\n",
       " -0.09715699669477362,\n",
       " 0.0829980474107388,\n",
       " 0.29605240744238137,\n",
       " -0.2689886127421637,\n",
       " -0.36867964014027144,\n",
       " -0.11104585783546587,\n",
       " -0.125613391428378,\n",
       " -0.1323188854184717,\n",
       " 0.01913401292807767,\n",
       " 0.10767492081694316,\n",
       " 0.4093636585118073,\n",
       " 0.19806173098032095,\n",
       " -0.17442544604254082,\n",
       " -0.31570887815385107,\n",
       " -0.23410255952390108,\n",
       " -0.40354659372865276,\n",
       " -0.005180029455596563,\n",
       " 0.6381757882882091,\n",
       " 0.6044272186248493,\n",
       " 0.012266920098658152,\n",
       " -0.5324726660970167,\n",
       " -0.30416929309063745,\n",
       " -0.07021373489970587,\n",
       " 0.1914201714853791,\n",
       " 0.2260116491591248,\n",
       " 0.02071239896987377,\n",
       " -0.21683675451293485,\n",
       " 0.05801015282476135,\n",
       " -0.029898993847192162,\n",
       " -0.23407784575112484,\n",
       " 0.10827323692465513,\n",
       " -0.1269457901192353,\n",
       " 0.4372492797400325,\n",
       " 0.44923925820999683,\n",
       " 0.09781874153301977,\n",
       " -0.6658837734991122,\n",
       " -0.2616129625241315,\n",
       " -0.21923047986521943,\n",
       " -0.39191100265223444,\n",
       " -0.3558832352684582,\n",
       " 0.33552792047970037,\n",
       " 0.09258122435676863,\n",
       " -0.3454303602215901,\n",
       " -0.07533027351497709,\n",
       " -0.07720449027251791,\n",
       " -0.19754740371607282,\n",
       " -0.34826127204125656,\n",
       " -0.08816374348912277,\n",
       " 0.09030944655729367,\n",
       " -0.18668682499564973,\n",
       " 0.08186557571869968,\n",
       " 0.14537621502308257,\n",
       " 0.3507554895023276,\n",
       " 0.8077389901356427,\n",
       " 0.5128694841848118,\n",
       " -0.3457724321618263,\n",
       " -0.201207351214637,\n",
       " -0.01812499039156771,\n",
       " 0.042533171434427024,\n",
       " 0.025918998935929475,\n",
       " 0.39427000568559695,\n",
       " 0.1680285191614829,\n",
       " 0.4165988477841178,\n",
       " 0.5254410810028631,\n",
       " 0.17547115599138416,\n",
       " 0.02578689525180429,\n",
       " 0.12246274823026952,\n",
       " -0.21034764622337618,\n",
       " -0.6177286239748788,\n",
       " -0.11365951084605147,\n",
       " -0.07155248324794855,\n",
       " -0.5158299161042452,\n",
       " -0.5027857750850254,\n",
       " -0.49906542547200156,\n",
       " 0.053053194914917806,\n",
       " -0.2655109389527102,\n",
       " -0.06169375031664101,\n",
       " 0.349412667875092,\n",
       " -0.1605297358917731,\n",
       " -0.3945510508538923,\n",
       " 0.1967741517863466,\n",
       " 0.03139095794228225,\n",
       " 0.5297816745578108,\n",
       " 0.8075555398041467,\n",
       " -0.037342465556103877,\n",
       " -0.3616664253722024,\n",
       " 0.16049164157606788,\n",
       " 0.26508836007873715,\n",
       " -0.06332822556327256,\n",
       " -1.0636447005675251,\n",
       " -0.7589589028152738,\n",
       " 0.22532500549192147,\n",
       " 0.3022320657707416,\n",
       " 0.30829168931341944,\n",
       " 0.25098240221542945,\n",
       " 0.26366358222651126,\n",
       " -0.2947892889449155,\n",
       " 0.30062102906562826,\n",
       " 0.19968374578457881,\n",
       " -0.01087161734801164,\n",
       " -0.5617159288503912,\n",
       " -0.44140401116365413,\n",
       " 0.004203629489780147,\n",
       " 0.8285752911026736,\n",
       " 0.5445649438019033,\n",
       " -0.4299673944673848,\n",
       " -0.5150990093357362,\n",
       " -0.29732896682464655,\n",
       " 0.018159091903042865,\n",
       " 0.008038051458770484,\n",
       " -0.12739913083887427,\n",
       " -0.36598943900229164,\n",
       " -0.31913075499504323,\n",
       " 0.27671746989250917,\n",
       " 0.21190027623221033,\n",
       " 0.33116348535137685,\n",
       " 0.07439132207013213,\n",
       " -0.022495047027681438,\n",
       " -0.35897270019831745,\n",
       " -0.03971753468874878,\n",
       " 0.0218825123280032,\n",
       " 0.13610302092153492,\n",
       " -0.17986643196783417,\n",
       " -0.6033884386865924,\n",
       " -0.3487487715060372,\n",
       " 0.26424134709946334,\n",
       " 0.09760321137879202,\n",
       " -0.02260129384741511,\n",
       " 0.28679222849224906,\n",
       " 0.03885779364911873,\n",
       " -0.09080565099455377,\n",
       " 0.12870642226365986,\n",
       " 0.3034662792942804,\n",
       " 0.24906586567528066,\n",
       " 0.14258136481310246,\n",
       " -0.06967352563081912,\n",
       " -0.11284324142905795,\n",
       " 0.32954405393496233,\n",
       " 0.049555815641649736,\n",
       " -0.17451517126323213,\n",
       " 0.0047321003560894526,\n",
       " 0.1188412998759663,\n",
       " 0.05628345662651597,\n",
       " 0.4708022920131214,\n",
       " -0.11524031890201675,\n",
       " -0.8089875614725872,\n",
       " -0.311058223573606,\n",
       " -0.16495667597521554,\n",
       " 0.6454026375277206,\n",
       " 0.31326502887674595,\n",
       " 0.15039115122138527,\n",
       " -0.03005931625690187,\n",
       " -0.5279640091714036,\n",
       " 0.08895969043116303,\n",
       " 0.2824700532258445,\n",
       " 0.4752769903156073,\n",
       " 0.12339129366626611,\n",
       " -0.3647914155034929,\n",
       " 0.01586941478919135,\n",
       " 0.17691359432134618,\n",
       " -0.0019770999415134882,\n",
       " -0.04303355089941337,\n",
       " -0.4756655425017814,\n",
       " 0.09768498623540484,\n",
       " 0.21251876142010892,\n",
       " 0.050543172478348886,\n",
       " -0.4441069834546722,\n",
       " -0.2324848194530115,\n",
       " -0.2740135136967602,\n",
       " -0.2851776465495841,\n",
       " 0.043812279084692915,\n",
       " 0.668257144153728,\n",
       " 0.7007427393660057,\n",
       " -0.2466170653823218,\n",
       " -0.5339916875083534,\n",
       " -0.5905285644862643,\n",
       " 0.14794129380689175,\n",
       " 0.2260329384780795,\n",
       " 0.17118123561419565,\n",
       " 0.06300939224600954,\n",
       " 0.28418968537197137,\n",
       " 0.4371657793340117,\n",
       " 0.08226788680002028,\n",
       " 0.4982530733156222,\n",
       " 0.159277938719588,\n",
       " -0.1266823445671042,\n",
       " -0.27902872635478904,\n",
       " -0.2928475052584637,\n",
       " -0.533174364324229,\n",
       " -0.14245784096982858,\n",
       " 0.06986258057958457,\n",
       " 0.23973893616264258,\n",
       " 0.13140640356375657,\n",
       " 0.01899121420260149,\n",
       " 0.7899140297700857,\n",
       " 0.37591317959592474,\n",
       " -0.17556195125285184,\n",
       " -0.9290275176308681,\n",
       " -0.8870369893242185,\n",
       " 0.5052798111221144,\n",
       " 1.2180293793780557,\n",
       " 0.9729988442197417,\n",
       " 0.13217950779266566,\n",
       " -0.12125269888340287,\n",
       " 0.4056684535992976,\n",
       " 0.06493171452363411,\n",
       " 0.10483842874099945,\n",
       " 0.3601481529435022,\n",
       " 0.7210103605686718,\n",
       " 0.19359683088722404,\n",
       " 0.016208623661328275,\n",
       " 0.47746142855680135,\n",
       " 0.012551332828590167,\n",
       " -0.18002542622842005,\n",
       " -0.49707178038746525,\n",
       " -0.4197350644163037,\n",
       " 0.021916361045102833,\n",
       " 0.18511486530397792,\n",
       " -0.07398934191278403,\n",
       " -0.03901473864638362,\n",
       " 0.4618388654600102,\n",
       " 0.28906119591886287,\n",
       " -0.2679687417005755,\n",
       " -0.2699509840009018,\n",
       " -0.6413101999284383,\n",
       " -0.5500204842470072,\n",
       " -0.011652271003792819,\n",
       " -0.16475954690197991,\n",
       " -0.2576354897628082,\n",
       " 0.23203514517295815,\n",
       " 0.18948034465571328,\n",
       " 0.5581038590040033,\n",
       " 0.4893991325599103,\n",
       " -0.32761127302868054,\n",
       " -0.7427340028823457,\n",
       " -0.25685292704898016,\n",
       " 0.36723547964007364,\n",
       " 0.4813469547734218,\n",
       " 0.5378664869522303,\n",
       " 0.28432450797114384,\n",
       " -0.5481866199138853,\n",
       " -0.3649395690231851,\n",
       " -0.044113584242312376,\n",
       " 0.31658764301445075,\n",
       " 0.24523953070730695,\n",
       " 0.32313885492218636,\n",
       " 0.2046382154105758,\n",
       " -0.12397595368338557,\n",
       " -0.22160888077309177,\n",
       " 0.0034131954134658526,\n",
       " 0.282558391281709,\n",
       " 0.8995414511439825,\n",
       " 0.9042395316210765,\n",
       " 0.06902254319204038,\n",
       " -0.3459011155284353,\n",
       " -0.24086416996864515,\n",
       " 0.0026711527280660896,\n",
       " -0.0028273298611633524,\n",
       " -0.5080703266071589,\n",
       " -0.8106341356397759,\n",
       " 0.029880156081586118,\n",
       " 0.5548246415331965,\n",
       " 0.33086129730207375,\n",
       " 0.3447864265153794,\n",
       " 0.2263350179717843,\n",
       " 0.09400062361998571,\n",
       " -0.19611480071600473,\n",
       " -0.0832874152936688,\n",
       " 0.36440364178783347,\n",
       " 0.6990201571480854,\n",
       " 0.748953691633331,\n",
       " 0.48105640464062127,\n",
       " -0.2116059194021671,\n",
       " -0.5345245512437526,\n",
       " -0.15639341355794953,\n",
       " 0.5574032155654232,\n",
       " 0.540772517823928,\n",
       " 0.29359560938280027,\n",
       " 0.019577219140018656,\n",
       " -0.9223911579658733,\n",
       " -1.2376580288098673,\n",
       " -0.2723720121904816,\n",
       " 0.45258868711348876,\n",
       " 0.0633984844253277,\n",
       " -0.008492581693861898,\n",
       " -0.5752087853183611,\n",
       " -0.778801530612728,\n",
       " -0.37662918933402423,\n",
       " 0.1693742874710334,\n",
       " 0.11438053744552776,\n",
       " -0.1364710690520518,\n",
       " 0.0758074152775457,\n",
       " 0.2525636551824515,\n",
       " -0.18677111378002972,\n",
       " 0.1998512298168365,\n",
       " 0.3188171807451737,\n",
       " -0.09502886782773953,\n",
       " -0.3776638871005542,\n",
       " -0.4598992901718922,\n",
       " 0.11168570739898318,\n",
       " -0.014652144854304328,\n",
       " 0.11118971747397265,\n",
       " 0.09213372859287389,\n",
       " -0.32780214729205237,\n",
       " -0.11889149150734285,\n",
       " -0.237020737972995,\n",
       " -0.5709104666194633,\n",
       " -0.37999298726249237,\n",
       " -0.10997921778318923,\n",
       " 0.5865435507450398,\n",
       " 0.08602555942689882,\n",
       " -0.33848792406445616,\n",
       " 0.008307702539385009,\n",
       " 0.8380578180647313,\n",
       " 0.8024369684516365,\n",
       " 0.39858058369288435,\n",
       " 0.19140623998751824,\n",
       " -0.16835847140639193,\n",
       " -0.44027112662610746,\n",
       " 0.2504627472834417,\n",
       " 0.28181930737861305,\n",
       " 0.6222941264769474,\n",
       " 0.5310290047530963,\n",
       " -0.037178107841151053,\n",
       " -0.6029814356766313,\n",
       " -0.8561318915223564,\n",
       " -0.8103102426891691,\n",
       " -0.4072504812584485,\n",
       " 0.3424894370084092,\n",
       " 0.15032514588941243,\n",
       " -0.16351342772001634,\n",
       " -0.35328529639376316,\n",
       " 0.29438232773900763,\n",
       " 0.11857238412362642,\n",
       " 0.05449211029500854,\n",
       " -0.41843966380028136,\n",
       " -0.23372365691371025,\n",
       " 0.48394972086786625,\n",
       " 0.7558935824037354,\n",
       " 0.5171501227704055,\n",
       " 0.31658180698482297,\n",
       " -0.011512285253905893,\n",
       " 0.022347628143639692,\n",
       " 0.45534392956176145,\n",
       " 0.6383889860454817,\n",
       " 0.533265753996581,\n",
       " -0.00832200650227835,\n",
       " -0.4032234143090457,\n",
       " -0.5568788322278014,\n",
       " 0.055895326311698745,\n",
       " 0.07104533318272252,\n",
       " 0.628529613632886,\n",
       " 0.8729284155791179,\n",
       " 0.5449776135496768,\n",
       " 0.12045808373400366,\n",
       " -0.043195982412593814,\n",
       " -0.523510799804692,\n",
       " -0.6132245947323577,\n",
       " -0.46041055742654724,\n",
       " -0.24376769757835484,\n",
       " -0.3101743488675909,\n",
       " -0.4874475423110297,\n",
       " -0.6870487122999676,\n",
       " -0.6666038418311397,\n",
       " -0.37812137577035254,\n",
       " 0.20596759554978503,\n",
       " 0.33510995017132805,\n",
       " 0.38129095714507455,\n",
       " 0.017643294728945658,\n",
       " -0.22711319807803554,\n",
       " -0.060054906345405196,\n",
       " 0.366994197662823,\n",
       " 0.3981768757231289,\n",
       " 0.19942133750319438,\n",
       " 0.022623102366948856,\n",
       " -0.24291682028331218,\n",
       " 0.18523781797049002,\n",
       " -0.058917972612722774,\n",
       " 0.3611938993300735,\n",
       " 0.574132033371171,\n",
       " 0.6033765653438689,\n",
       " 0.21279506580097168,\n",
       " 0.1294739609099758,\n",
       " -0.4656514474387458,\n",
       " -0.8324663919799886,\n",
       " -0.6461205471665314,\n",
       " -0.5419536967334043,\n",
       " -0.3331644673256608,\n",
       " -0.28011034991576156,\n",
       " -0.27522887103635996,\n",
       " -0.00400810812219439,\n",
       " -0.15459128105064057,\n",
       " -0.29184234451835867,\n",
       " -0.3496673131373581,\n",
       " 0.1274281853463498,\n",
       " 0.21670118553711526,\n",
       " 0.03428081335030911,\n",
       " -0.1005107519832024,\n",
       " -0.5358903616869146,\n",
       " -0.4658933150742,\n",
       " 0.10419083198408571,\n",
       " 0.34586434219060014,\n",
       " -0.05455371463755174,\n",
       " -0.8856840202901858,\n",
       " -0.8964059303989353,\n",
       " -0.46442183879873894,\n",
       " 0.14462907703218086,\n",
       " 0.42174574199447445,\n",
       " -0.05788123415705143,\n",
       " -0.13422221433557943,\n",
       " 0.5834223521968365,\n",
       " 0.3198782925664292,\n",
       " 0.45537227187192836,\n",
       " -0.07059981910264354,\n",
       " -0.03173822355005282,\n",
       " -0.36773645835090496,\n",
       " -0.28249311651634085,\n",
       " -0.3146808581608632,\n",
       " 0.366676516434403,\n",
       " 0.13037111555308178,\n",
       " 0.7174938073633453,\n",
       " 0.33547452057237626,\n",
       " 0.015426365993944584,\n",
       " -0.16589845780062185,\n",
       " -0.505690033425831,\n",
       " -0.1424587933748561,\n",
       " 0.5227712926449402,\n",
       " 0.16418905181598764,\n",
       " 0.21663299121405047,\n",
       " -0.1490436018132829,\n",
       " -0.7846343050502731,\n",
       " -0.6119603274152645,\n",
       " -0.07006652715643824,\n",
       " 0.3432635108213212,\n",
       " 0.24631781548832363,\n",
       " 0.7308836852341405,\n",
       " 0.5055304154664968,\n",
       " 0.39165124818490293,\n",
       " -0.08131760876329444,\n",
       " -0.2444993266818578,\n",
       " 0.008976448506014506,\n",
       " 0.20740249178194614,\n",
       " -0.36998274554185895,\n",
       " -0.21233929485118166,\n",
       " 0.5659230560097702,\n",
       " 0.36424870451282143,\n",
       " -0.02332343890620041,\n",
       " -0.17109356377466425,\n",
       " -0.21861598476365768,\n",
       " -0.07825587812558416,\n",
       " 0.19018382473862372,\n",
       " 0.3645835525160621,\n",
       " -0.3410850952189167,\n",
       " -0.06193984442059819,\n",
       " 0.5829189961085258,\n",
       " 0.38287359785890235,\n",
       " 0.0700281358955099,\n",
       " -0.08034416312144906,\n",
       " -0.10555026696475772,\n",
       " -0.5880912023311432,\n",
       " -0.54284773983108,\n",
       " 0.15749011163178123,\n",
       " 1.0337189146533328,\n",
       " 0.38873601569699284,\n",
       " 0.003206605702501128,\n",
       " -0.47813421662902944,\n",
       " -0.43707299574256997,\n",
       " -0.4742999171846661,\n",
       " -0.688803307879238,\n",
       " -0.702567565746496,\n",
       " -0.6336660348748538,\n",
       " -0.09778689465335735,\n",
       " 0.10338103560044978,\n",
       " -0.3344313509005357,\n",
       " -0.758755312884432,\n",
       " -0.8284076084292857,\n",
       " -0.29085381603321536,\n",
       " -0.04673712264839183,\n",
       " 0.1843795428462741,\n",
       " 0.08564190691400801,\n",
       " -0.14700363366615826,\n",
       " -0.5507316001173429,\n",
       " -0.013331738167166374,\n",
       " 0.013170991626577089,\n",
       " -0.23689721342543132,\n",
       " 0.05099659089406486,\n",
       " -0.058261338232355686,\n",
       " 0.03306151140196889,\n",
       " 0.31484597022578087,\n",
       " -0.08846115236002955,\n",
       " -0.059368314342311435,\n",
       " -0.7119643754923936,\n",
       " -0.30611077134566256,\n",
       " 0.21506601499423225,\n",
       " 0.4017712932673223,\n",
       " 0.017586765603898102,\n",
       " -0.4045310786670608,\n",
       " -0.43578005074577986,\n",
       " 0.5741850449586889,\n",
       " 0.2031387164406684,\n",
       " -0.278915030034922,\n",
       " -0.05735073637683086,\n",
       " -0.05948176232331806,\n",
       " -0.08179908769231103,\n",
       " 0.3813106350073593,\n",
       " 0.20603279291485802,\n",
       " 0.11644001256675396,\n",
       " -0.22092921673877655,\n",
       " -0.1797151183710725,\n",
       " -0.28298063444373667,\n",
       " 0.45507491515063525,\n",
       " 0.37738331062492664,\n",
       " -0.4712607351091128,\n",
       " -0.6634904865093054,\n",
       " -0.34795020688982237,\n",
       " -0.5569658102510081,\n",
       " -0.3029562068458183,\n",
       " 0.2410466654008284,\n",
       " 0.15932762742214165,\n",
       " 0.6105284180369334,\n",
       " 0.9492086960967336,\n",
       " 0.658756070309235,\n",
       " 0.0856683648023912,\n",
       " -0.5038950879917362,\n",
       " -0.5310147427693985,\n",
       " -0.4070688618788699,\n",
       " -0.11955506943457608,\n",
       " 0.05513516725972581,\n",
       " 0.4690727402999916,\n",
       " 0.0059458799750176095,\n",
       " -0.38301753346054157,\n",
       " -0.055046750492569185,\n",
       " 0.0885955992239456,\n",
       " 0.04413034104124531,\n",
       " 0.2913990077759535,\n",
       " 0.8341550558826879,\n",
       " 0.6708381233783018,\n",
       " 0.2886365879291672,\n",
       " -0.3938376601898174,\n",
       " -0.2642009267070792,\n",
       " 0.13283567514206354,\n",
       " 0.5721626018566807,\n",
       " 0.421942190032844,\n",
       " 0.28263861661501494,\n",
       " 0.10805079052662046,\n",
       " 0.02407689222901243,\n",
       " -0.24939744036378803,\n",
       " -0.306632780613225,\n",
       " -0.04510215099128098,\n",
       " 0.8641254751198509,\n",
       " 0.4314475443032507,\n",
       " 0.2685178883411721,\n",
       " 0.10733245629782763,\n",
       " 0.07427726848162734,\n",
       " -0.05496564545888897,\n",
       " 0.5288743519552072,\n",
       " 0.9183819931292927,\n",
       " 0.5655181615300725,\n",
       " -0.0742087477283741,\n",
       " -0.4788027873710999,\n",
       " -0.3387966353341269,\n",
       " 0.23866142561124312,\n",
       " 0.6207193981062338,\n",
       " 0.3472475610490975,\n",
       " -0.15885036064693,\n",
       " -0.41265899305166104,\n",
       " -0.6631862805101292,\n",
       " -0.7062989394117504,\n",
       " -0.49217731798676323,\n",
       " -0.07644668890343459,\n",
       " 0.1115088981703862,\n",
       " -0.02852779402502087,\n",
       " 0.0169561028001617,\n",
       " -0.5594987711291168,\n",
       " -0.7959860373967049,\n",
       " -0.4120599387431648,\n",
       " -0.21050509518558957,\n",
       " -0.3500601283626754,\n",
       " -0.012492426239034432,\n",
       " 0.23506974397959762,\n",
       " -0.20941907469316995,\n",
       " -0.47448527752211134,\n",
       " -0.45741445255944413,\n",
       " -0.6470949073070567,\n",
       " -0.12355316197529526,\n",
       " -0.41647068049323965,\n",
       " -0.017751616824794036,\n",
       " 0.2783324247411323,\n",
       " -0.08575382288289268,\n",
       " 0.09604501959885026,\n",
       " -0.06591886866915778,\n",
       " 0.013461273314097397,\n",
       " -0.28490311671498103,\n",
       " -0.3417058426561952,\n",
       " -0.26080989545489686,\n",
       " -0.20120007549294308,\n",
       " -0.8255736007927763,\n",
       " -0.4111731475550244,\n",
       " -0.15877305700918948,\n",
       " -0.1104067964608173,\n",
       " -0.2791583525350477,\n",
       " -0.6926132343402123,\n",
       " -0.4758409857860194,\n",
       " -0.2763495663275646,\n",
       " 0.6058444734375111,\n",
       " 0.42327455531045666,\n",
       " -0.11057763613882243,\n",
       " -0.41473971372015433,\n",
       " -0.31172983273535654,\n",
       " 0.2987298431975821,\n",
       " 0.6966195597230785,\n",
       " -0.21904336789938805,\n",
       " -0.3977085840904274,\n",
       " -0.7617400630798306,\n",
       " -0.2921967996083777,\n",
       " -0.36636992456286305,\n",
       " -0.12443333430377253,\n",
       " -0.3530913420600867,\n",
       " -0.322612722960118,\n",
       " -0.23477630135544258,\n",
       " 0.3349823600875832,\n",
       " -0.3455400626445225,\n",
       " -0.5909352586108484,\n",
       " 0.09909341901276686,\n",
       " 0.6831120175632328,\n",
       " 0.6986382880687148,\n",
       " 0.36931227912754955,\n",
       " 0.12721190094784796,\n",
       " 0.10660450857037633,\n",
       " 0.007800964178577445,\n",
       " -0.19383284810423135,\n",
       " -0.38612545169083445,\n",
       " -0.28356710335302643,\n",
       " -0.1999808614021367,\n",
       " -0.2717466326726511,\n",
       " -0.09784271119447502,\n",
       " -0.005504257875035515,\n",
       " -0.39314316437364905,\n",
       " -0.5750891073036175,\n",
       " -0.18733078480086324,\n",
       " -0.09208972419300629,\n",
       " 0.3105152795087109,\n",
       " 0.20417285912949767,\n",
       " 0.2006560383553887,\n",
       " 0.3066291755757953,\n",
       " 0.2854847067982082,\n",
       " 0.4230392545755787,\n",
       " -0.5080052288307386,\n",
       " -0.4310258995112461,\n",
       " -0.14981104782969315,\n",
       " -1.0223952609171525,\n",
       " -1.0662054329050648,\n",
       " -0.6607210390874372,\n",
       " -0.30633292829988445,\n",
       " 0.009495137273466046,\n",
       " -0.1897406308126981,\n",
       " -0.19437737938798202,\n",
       " -0.21827696895034873,\n",
       " 0.2868988946194655,\n",
       " 0.11506525778426177,\n",
       " -0.1409592251466929,\n",
       " -0.17569023245186416,\n",
       " -0.11724831564933268,\n",
       " 0.10101127117663708,\n",
       " 0.08595443549896942,\n",
       " -0.13900439874803164,\n",
       " -0.049594434238201604,\n",
       " -0.26539140770209374,\n",
       " -0.2472921765504589,\n",
       " 0.11175265809220018,\n",
       " -0.2082330650571188,\n",
       " -0.17421057484747549,\n",
       " 0.21794993869973134,\n",
       " 0.5854006180708446,\n",
       " 0.25544793017799416,\n",
       " 0.4405887925166684,\n",
       " 0.42675057943208716,\n",
       " 0.3594396229759157,\n",
       " 0.25218564552994394,\n",
       " -0.028028315093023934,\n",
       " 0.1103191163574359,\n",
       " -0.06119930993188488,\n",
       " 0.3228387864257021,\n",
       " 0.42376630215156763,\n",
       " 0.39113667478382685,\n",
       " 0.20451747847276003,\n",
       " 0.14722427891820683,\n",
       " 0.44062350226598623,\n",
       " 0.47544269839327924,\n",
       " 0.3413274161264866,\n",
       " -0.1592805157055881,\n",
       " -0.6551364772102125,\n",
       " -0.6301235068732698,\n",
       " 0.34441189450899035,\n",
       " 0.23664775559869533,\n",
       " 0.017337061040323565,\n",
       " -0.032565782082908355,\n",
       " 0.2653713797471094,\n",
       " -0.12102963620759716,\n",
       " -0.24851797349501792,\n",
       " 0.46821237672794325,\n",
       " 0.38190035993538274,\n",
       " 0.15137154398854313,\n",
       " 0.012472435773917559,\n",
       " -0.1404043874945132,\n",
       " -0.3412635836022508,\n",
       " 0.5620951182291967,\n",
       " 0.6549462020546651,\n",
       " 0.29577968743917576,\n",
       " -0.027266232682640872,\n",
       " 0.014856408477699617,\n",
       " -0.06486227583646065,\n",
       " -0.2814728725711227,\n",
       " -0.37635471963755374,\n",
       " 0.6765550241428375,\n",
       " 0.9850348892692791,\n",
       " 0.9961451009673362,\n",
       " 0.5732222761716111,\n",
       " 0.4629206041397194,\n",
       " 0.35224855941439115,\n",
       " -0.20985921391667509,\n",
       " 0.22123982428079703,\n",
       " 0.002372094969533556,\n",
       " -0.07072732492100964,\n",
       " 0.5595283813183791,\n",
       " 1.5069372903349352,\n",
       " 1.2863609600941082,\n",
       " 0.8061686783897553,\n",
       " -0.45526355345101865,\n",
       " 0.07441186097628638,\n",
       " 0.5076380860053088,\n",
       " 0.13943382943936192,\n",
       " -1.0369472850071222,\n",
       " -1.2949378446677628,\n",
       " -1.1556578732884515,\n",
       " 0.42463479284632766,\n",
       " 0.6677049521950913,\n",
       " -0.051330756386868515,\n",
       " -0.3114962471567695,\n",
       " -0.385885213109878,\n",
       " 0.12074042953944225,\n",
       " 0.02774254454054137,\n",
       " 0.5069519074004015,\n",
       " 0.10105443428930133,\n",
       " -0.24995893082617782,\n",
       " -0.2838415338023678,\n",
       " -0.2605377850855043,\n",
       " -0.7405344264543456,\n",
       " -0.588974357395623,\n",
       " -0.0355052896920392,\n",
       " -0.1168213895346257,\n",
       " 0.09816632417790626,\n",
       " 0.10581235279881038,\n",
       " 0.5011450742062481,\n",
       " 0.6323158355321101,\n",
       " 0.1656280540555912,\n",
       " 0.04019480678153034,\n",
       " -0.38894517792965017,\n",
       " -0.4905271024321401,\n",
       " -0.009662718451770469,\n",
       " -0.4217682318940259,\n",
       " 0.46585498479559523,\n",
       " 0.7726707969585922,\n",
       " 0.667142677388859,\n",
       " -0.34493712324892545,\n",
       " -0.4163532917971926,\n",
       " -0.31432566876945855,\n",
       " -0.6481957515924507,\n",
       " -0.47890431826152,\n",
       " -0.4464707509586522,\n",
       " -0.03824140487050559,\n",
       " 0.6396760062389638,\n",
       " 0.7661629528447786,\n",
       " 0.7861386260546304,\n",
       " -0.046081649442041905,\n",
       " -0.7104223951803323,\n",
       " -1.0163084172568584,\n",
       " -0.33335933200507395,\n",
       " 0.2796942572217261,\n",
       " -0.06252323397218323,\n",
       " 0.13126116927911585,\n",
       " 0.0934712379990552,\n",
       " 0.32062670382225134,\n",
       " 0.10863373595610248,\n",
       " 0.12638726516486454,\n",
       " -0.10321632758734986,\n",
       " -0.06019209938518244,\n",
       " -0.027602001942134663,\n",
       " -0.07665612935941166,\n",
       " -0.235952779501736,\n",
       " -0.4813869195963356,\n",
       " 0.04468192128529369,\n",
       " 0.5302245570032535,\n",
       " 0.24445407281877238,\n",
       " -0.45534966784386877,\n",
       " -0.21922928149705528,\n",
       " -0.3614876917022142,\n",
       " 0.2814034425110198,\n",
       " 0.46064673546154233,\n",
       " 0.48217248560511594,\n",
       " 0.09212604158563026,\n",
       " 0.02973912841142634,\n",
       " -0.3677131965896883,\n",
       " -0.7944372263718331,\n",
       " 0.05560959148738304,\n",
       " 0.3079586814519034,\n",
       " -0.037662944318249125,\n",
       " -0.03613635104835167,\n",
       " -0.22703428412951793,\n",
       " -0.7086624017302535,\n",
       " -0.7095401233459624,\n",
       " -0.16026513887041133,\n",
       " 0.7132409452729174,\n",
       " 0.6174233863911099,\n",
       " 0.0010369789041290434,\n",
       " -0.7363202391933298,\n",
       " -0.6034946091865313,\n",
       " 0.027232078529630444,\n",
       " -0.03841116473693068,\n",
       " -0.6288618678050544,\n",
       " -0.345582129240512,\n",
       " -0.044442389851536354,\n",
       " 0.4928769414783122,\n",
       " 0.6352814288874092,\n",
       " -0.10289214320741485,\n",
       " ...]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.00616093,  0.78446347, -0.40847891, -0.01364148,  0.01496872,\n",
       "        0.01429462])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [86]: train = pd.read_csv('datasets/titanic/train.csv')\n",
    "In [87]: test = pd.read_csv('datasets/titanic/test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId      0\n",
       "Survived         0\n",
       "Pclass           0\n",
       "Name             0\n",
       "Sex              0\n",
       "Age            177\n",
       "SibSp            0\n",
       "Parch            0\n",
       "Ticket           0\n",
       "Fare             0\n",
       "Cabin          687\n",
       "Embarked         2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId      0\n",
       "Pclass           0\n",
       "Name             0\n",
       "Sex              0\n",
       "Age             86\n",
       "SibSp            0\n",
       "Parch            0\n",
       "Ticket           0\n",
       "Fare             1\n",
       "Cabin          327\n",
       "Embarked         0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "impute_value = train.Age.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "train.Age.fillna(impute_value,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "test.Age.fillna(impute_value,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "train['IsFemale'] = (train.Sex == 'female').astype('i1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "test['IsFemale'] = (test.Sex== 'female').astype('i1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictors = ['Pclass','IsFemale','Age']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = train[predictors].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test = test[predictors].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train = train.Survived.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = LogisticRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
       "          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n",
       "          verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict = model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 1], dtype=int64)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch',\n",
       "       'Ticket', 'Fare', 'Cabin', 'Embarked', 'IsFemale'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',\n",
       "       'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked', 'IsFemale'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegressionCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_cv = LogisticRegressionCV(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegressionCV(Cs=10, class_weight=None, cv=None, dual=False,\n",
       "           fit_intercept=True, intercept_scaling=1.0, max_iter=100,\n",
       "           multi_class='ovr', n_jobs=1, penalty='l2', random_state=None,\n",
       "           refit=True, scoring=None, solver='lbfgs', tol=0.0001, verbose=0)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_cv.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = LogisticRegression(C = 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = cross_val_score(model,X_train,y_train,cv = 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.77232143, 0.80269058, 0.77027027, 0.78828829])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
